﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Windows.Input;
using BaMusaSoft.RPS.DataModel;
using BaMusaSoft.RPS.DataModel.Repositories;
using BaMusaSoft.RPS.ViewModel.Infrastructure;
using BaMusaSoft.RPS.ViewModel.Properties;
using GalaSoft.MvvmLight.Command;

namespace BaMusaSoft.RPS.ViewModel
{
    public class AddSignerViewModel:Workspace
    {

        #region "Fields"

        private readonly RepositoryBase<Signer> _signersRepository;
        private readonly IUnitOfWork _unitOfWork;
        private ObservableCollection<Signer> _signers;
        private RelayCommand _searchCommand;
        private RelayCommand _refreshCommand;
        private Signer _selectedSigner;
        #endregion

        #region "Constructor"

        public AddSignerViewModel(IUnitOfWork unitOfWork)
        {
            if (unitOfWork == null) throw new ArgumentNullException("unitOfWork");
            _unitOfWork = unitOfWork;
            _signersRepository = _unitOfWork.Signers;
            Signers = new ObservableCollection<Signer>(_signersRepository.GetAll());
            DisplayName = Resources.SignerViewModel_DisplayName;
        }

        #endregion

        #region "Properties"

        public string IdNumber { get; set; }
        [Bindable(true)]
        public ObservableCollection<Signer> Signers
        {
            get { return _signers; }
            private set
            {
                _signers = value;
                RaisePropertyChanged("Signers");
            }
        }

        public Signer SelectedSigner
        {
            get { return _selectedSigner; }
            set
            {
                _selectedSigner = value;
                RaisePropertyChanged("SelectedSigner");
            }
        }

        #endregion

        #region "Commands"

        public ICommand SearchCommand
        {
            get { return _searchCommand ?? (_searchCommand = new RelayCommand(SearchSigners)); }
        }

        public ICommand RefreshCommand
        {
            get { return _refreshCommand ?? (_refreshCommand = new RelayCommand(RefreshSigners)); }
        }
        #endregion

        #region "Methods"

        private void SearchSigners()
        {
            if (!string.IsNullOrEmpty(IdNumber))
            {
                IEnumerable<Signer> temp =
                    _signersRepository.GetAll().Where(x => x.IdNumber.Contains(IdNumber));
                Signers = new ObservableCollection<Signer>(temp);

            }
            else
            {
                Signers = new ObservableCollection<Signer>(_signersRepository.GetAll());
            }
        }
        void RefreshSigners()
        {
            _signersRepository.Refresh();
            RefreshBinding();
        }

        #endregion

        #region Overrides of Workspace

        public sealed override string DisplayName
        {
            get; protected set;
        }

        public override bool HasChanges
        {
            get { throw new System.NotImplementedException(); }
        }

        #endregion

        #region "Helpers"
        private void RefreshBinding()
        {
            IEnumerable<PropertyInfo> bindableProperties = from p in typeof(AddSignerViewModel).GetProperties()
                                                           where
                                                               p.GetCustomAttributes(true).Cast<Attribute>().Contains(
                                                                   new BindableAttribute(true))
                                                           select p;
            foreach (PropertyInfo p in bindableProperties)
            {
                RaisePropertyChanged(p.Name);
            }
        }
        #endregion

    }
}